package com.ecs.core.map;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.utils.IntMap;
import com.badlogic.gdx.math.MathUtils;
import com.ecs.core.debug.Debug;
/**
 * 地形加载器
 * @Author 天下无敌
 * @Date 2025/05/04 15:24
 */
public class TerrainManager implements Disposable{
    private static final String TERRAINS_DIR = "terrains/"; // 默认目录

    private final AssetManager assetManager;
    private final IntMap<TerrainData> terrainsById = new IntMap<>();
	private final IntMap<TreeData> treesById = new IntMap<>();
	
    public TerrainManager(AssetManager assetManager) {
        this.assetManager = assetManager;
        loadAllTerrains();
    }

    //加载所有地形数据 
    private void loadAllTerrains() {
        FileHandle dir = Gdx.files.internal(TERRAINS_DIR);
        if (!dir.exists()) {
            throw new RuntimeException("地形目录不存在: " + dir.path());
        }

        for (FileHandle file : dir.list()) {
			if(file.isDirectory() && file.name().equals("trees")){
				loadAllTrees(file);
			}else if (file.extension().equals("json")) {
                try {
                    TerrainData terrain = TerrainData.loadFromJson(assetManager, file.path());
                    terrainsById.put(terrain.terrainId, terrain);
                } catch (Exception e) {
					throw new RuntimeException("加载失败"+ file.path(),e);
                }
            }
        }
    }

	private void loadAllTrees(FileHandle dir){
        for (FileHandle file : dir.list()) {
            if (file.extension().equals("json")) {
                try {
                    TreeData tree = TreeData.loadFromJson(assetManager, file.path());
                    treesById.put(tree.treeId, tree);
					Debug.log("Load",file.name());
                } catch (Exception e) {
					throw new RuntimeException("加载失败"+ file.path(),e);
                }
            }
        }
	}
    /** 通过 terrainId 获取地形 */
    public TerrainData getById(int terrainId) {
        return terrainsById.get(terrainId);
    }
	//树木id
	public TreeData getTreeById(int terrainId) {
        return treesById.get(terrainId);
    }
	
	public TreeData getRandomTrre(int terrainid){
		TreeData data = null;
		for(TreeData d:getAllTrees()){
			if(d.terrainId == terrainid){
				if(data == null){
					data = d;
				}else{
					data = MathUtils.randomBoolean() ? data:d;
				}
			}
		}
		return data;
	}
	//获取所有树木
	public Array<TreeData> getAllTrees(){
		return treesById.values().toArray();
	}
	/** 获取所有地形 */
    public Array<TerrainData> getAllTerrains() {
        return terrainsById.values().toArray();
    }
	
	public TerrainData matchesEnvironment(float height, float moisture, float temperature) {
		for(TerrainData data:getAllTerrains()){
			if(data.matchesEnvironment(height)){
				return data;
			}
		}
		return getAllTerrains().get(0);
	}
	
	
	@Override
	public void dispose()
	{
		for(TerrainData data:terrainsById.values()){
			data.region = null;
		}
		for(TreeData data:treesById.values()){
			data.deadman = null;
			data.grow = null;
		}
		terrainsById.clear();
	}

}
